#include "esp32_s3_btn.h"


static const char *TAG = "ESP32_S3_BTN";

/***********************************************************/
/****************    物理按键 ↓   ****************************/

// // 按钮回调函数
// void button_event_cb(void *arg, void *data) {
//     button_callback_data_t *callback_data = (button_callback_data_t *)data;
//     if (callback_data == NULL)
//     {
//         // Handle memory allocation failure
//         return;
//     }

//     uint32_t button_num = callback_data->button_num;
//     button_event_t event = callback_data->event;
// }

// 按钮初始化, 设置按钮引脚与设置回调函数
void button_init(uint32_t button_num, button_event_t event, button_cb_t callback_func)
{
    button_callback_data_t *callback_data = malloc(sizeof(button_callback_data_t));
    if (callback_data == NULL)
    {
        // Handle memory allocation failure
        return;
    }
    callback_data->event = event;
    callback_data->button_num = button_num;

    // 设置按键长安或短按的毫秒数
    button_config_t btn_cfg = {0};
    // 设置gpio参数
    button_gpio_config_t gpio_cfg = {
        .gpio_num = button_num,
        .active_level = BUTTON_ACTIVE_LEVEL,
        .disable_pull = true,
        .enable_power_save = false,
    };
    // 创建按钮
    button_handle_t btn;
    esp_err_t ret = iot_button_new_gpio_device(&btn_cfg, &gpio_cfg, &btn);
    ESP_ERROR_CHECK(ret);
    // 设置按钮回调
    esp_err_t err = iot_button_register_cb(btn, event, NULL, callback_func, callback_data);
    ESP_ERROR_CHECK(err);
}


// void io0_btn_cb_short(void *arg, void *data) {
//     ESP_LOGI(TAG, "io0_btn_cb_short ........ ");  // 打印信息
// }

// void io0_btn_init(void){
//     button_init(GPIO_NUM_0, BUTTON_SINGLE_CLICK, io0_btn_cb_short);

// }

// 初始化GPIO引脚
void io0_gpio_init(void) {
    ESP_ERROR_CHECK(gpio_reset_pin(GPIO_NUM_0));
    ESP_ERROR_CHECK(gpio_set_direction(GPIO_NUM_0, GPIO_MODE_INPUT));
    ESP_ERROR_CHECK(gpio_pulldown_en(GPIO_NUM_0));
    ESP_LOGI(TAG, "GPIO_NUM_0 was set to input and pulldown"); 
}

/***************    物理按键 ↑   *************************/
/***********************************************************/